package Server;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import Network.Protocol;

public class Database {
	static Database database = null;

	Connection con;

	private static void databaseFactory() {
		if (database == null) {
			database = new Database();
		}
	}

	public static Database getDatabase() {
		databaseFactory();
		return database;
	}

	private Database() {
		String url = "jdbc:oracle:thin:@203.252.164.175:1521:orcl";

		try {
			Class.forName("oracle.jdbc.driver.OracleDriver");
			con = DriverManager.getConnection(url, "scott", "tiger");
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

	public int selectLoginQuery(String id, String password) {
		String sql = "SELECT password FROM user_account WHERE user_id = '" + id
				+ "'";
		Statement stmt;
		try {
			stmt = con.prepareStatement(sql);
			ResultSet rs = stmt.executeQuery(sql);
			while (rs.next()) {
				if (rs.getString("password").equals(password)) {
					return Protocol.PrtAccept;
				} else {
					return Protocol.PrtPWerror;
				}
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}

		return Protocol.PrtIDerror;
	}
}
